package com.example.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.entity.AccountDO;
import com.example.mapper.AccountMapper;
import com.example.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
//@AllArgsConstructor
public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountDO> implements AccountService {

//    @Autowired
//    private final AccountMapper accountMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void debit(Integer id, Long amount) {
        log.info("准备扣除用户id:[{}][{}]分钱",id,amount);
//        LambdaUpdateWrapper<AccountDO> wrapper = Wrappers.lambdaUpdate(AccountDO.class)
//                .eq(AccountDO::getId,id)
//                .gt(AccountDO::getBalance,0)
//                .setSql("balance = balance - " +amount);
        AccountDO accountDO = new AccountDO();
        accountDO.setId(id);
        AccountDO temp = baseMapper.selectById(id);
        Long balance = temp.getBalance();
        accountDO.setBalance(balance - amount);
        int result = baseMapper.updateById(accountDO);
        if (result < 0){
            throw new RuntimeException("余额不足");
        }else{
            System.out.println("成功扣除");
        }

    }

}
